<?php

/**
 *栏目管理
 *@author huuwhois
 *@time 2017/11/17
*/
namespace app\admin\controller;

use app\admin\model\Category as CateModel;
use think\Request;

class Category extends Base
{
    protected $model;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->model = new CateModel();
    }

    /**
     * 栏目列表
    */
    public function index()
    {
        $data = $this->model->field('id, pid, name, template, status, sort, description')->select();

        $data = obj_tree($data);
    
        $this->assign("data", $data);
        return $this->fetch();
    }

    public function add()
    {
        if (Request::instance()->isPOST()) {
            $param= input('post.');
            // var_dump($param);
            // exit;
            $name =  $param['name'];

            if ($name == '') {
                $this->error("栏目名不能为空");
            }

            $banner = isset($param['banner']) ? trim($param['banner']) : '';
            $pid = isset($param['pid']) ? intval($param['pid']) : 0;
            $template = isset($param['template']) ? intval($param['template']) : 0;
            $sort = isset($param['sort']) ? intval($param['sort']) : 0;
            $description = isset($param['description']) ? trim($param['description']) : '';
            $mtime = time();
    
            try {
                $this->model->save([
                    'banner' => $banner,
                    'pid' => $pid,
                    'name' => $name,
                    'template' => $template,
                    'sort' => $sort,
                    'description' => $description,
                    'mtime' => $mtime
                ]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误代码:".$msg);
            }

            $this->success('添加成功', 'index');
        } else {
            $data_category =  $this->model->where('status', 1)->field('id, pid, name')->select();

            $data_category = obj_tree($data_category);

            // $templet = model('templet')->where('status', 1)->column('id, name');
            // $this->assign('templet', $templet);
            
            $this->assign('data_category', $data_category);
            return $this->fetch();
        }
    }

    public function edit($id = "")
    {
        $data = $this->model->get($id);
        
        if (Request::instance()->isPOST()) {
            $param= input('post.');
            // var_dump($param);
            // exit;
            $name =  $param['name'];

            if ($name == '') {
                $this->error("栏目名不能为空");
            }

            $banner = isset($param['banner']) ? trim($param['banner']) : '';
            $pid = isset($param['pid']) ? intval($param['pid']) : 0;
            $template = isset($param['template']) ? intval($param['template']) : 0;
            $sort = isset($param['sort']) ? intval($param['sort']) : 0;
            $status = isset($param['status']) ? intval($param['status']) : 0;
            $description = isset($param['description']) ? trim($param['description']) : '';
            $mtime = time();

            if ($name == '') {
                $this->error("栏目名不能为空");
            }

            if ($pid == $id) {
                $this->error("上级栏目不能为自己");
            }

            $ids = $this->model->getIds($id);

            if (in_array($pid, $ids)) {
                $this->error("上级栏目不能为自己的子栏目");
            }

            try {
                $this->model->save([
                    'banner' => $banner,
                    'pid' => $pid,
                    'name' => $name,
                    'template' => $template,
                    'sort' => $sort,
                    'status' => $status,
                    'description' => $description,
                    'mtime' => $mtime
                ], ['id' => $id]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误提示:".$msg);
            }
            $this->success('编辑成功', 'index');
        } else {
            $top = $this->model->where('pid', 0)->column('id, name');

            $this->assign('top', $top);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    public function del($id = null)
    {
        if (Request::instance()->isAjax()) {
            $result = $this->model->where('pid', 'IN', $id)->select();
            
            if ($result) {
                $this->error('此栏目有子栏目, 不可删除, 若要删除请先删除 or 移除子栏目！');
            }

            // $result = model('article')->where('cid', 'IN', $id)->select();
            
            // if ($result) {
            //     $this->error('此栏目文章非空, 不可删除, 若要删除请先删除 or 移除其下文章！');
            // }
            $result = $this->model->destroy($id);
            
            if (!$result) {
                return ['code'=>0, 'msg'=>'删除失败'];
            } else {
                return ['code'=>2, 'msg'=>'删除成功'];
            }
        }
    }
}
